﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;
using System.IO;


namespace VoiceTestPlatform
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        Timer timer;
        MainViewModel vm;
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
            timer = new Timer(2000);
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Start();
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() =>
            {
                txtRev.ScrollToEnd();
            }));
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
             vm = new MainViewModel();
            vm.Load();
            this.DataContext = vm;


            //C#中文乱码转换

            //string text="中文";
            //string keyword;
            //byte[] buffer= Encoding.UTF8.GetBytes(text);
            //keyword=Encoding.GetEncoding("GB2312").GetString(buffer);
            //string codename = Encoding.Default.EncodingName;
            //Encoding eee = Encoding.Default;
            //var buffer = Encoding.Default.GetBytes(AppDomain.CurrentDomain.BaseDirectory);
            //ParameterManager.RunDir = Encoding.UTF8.GetString(buffer);// System.IO.Directory.GetCurrentDirectory();
            ParameterManager.RunDir = AppDomain.CurrentDomain.BaseDirectory;
            //MessageBox.Show(ParameterManager.RunDir);
            try
            {
                string[] sounds = Directory.GetFiles(ParameterManager.RunDir, "*.wav");
                if (sounds != null)
                {
                    foreach (var item in sounds)
                    {
                        File.Delete(item);
                    }

                }
            }
            catch (Exception ex)
            {

                //throw;
            }

        }

        private void ItemsControl_Drop(object sender, DragEventArgs e)
        {
            //string dataString = (string)e.Data.GetData(DataFormats.StringFormat);
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            // Assuming you have one file that you care about, pass it off to whatever
            // handling code you have defined.
            //HandleFileOpen(files[0]);
        }

        private void PortSetting_Click(object sender, RoutedEventArgs e)
        {
            if (PortSetting.Visibility == System.Windows.Visibility.Visible)
            {
                PortSetting.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                PortSetting.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var fe = e.OriginalSource as FrameworkElement;
            if (fe != null)
            {
                var data = fe.DataContext as Sound;
                if (data != null)
                {
                    ProjectManager.AddProjectCommand.Execute(data);
                }

            }

        }

        private void ic_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (!vm.IsPlayProjectEnabled)
            {
                return;
            }
            var fe = e.OriginalSource as FrameworkElement;
            if (fe != null)
            {
                var data = fe.DataContext as ProjectItem;
                if (data != null)
                {
                    int index = ProjectManager.ProjectItems.IndexOf(data);
                    ProjectManager.PlayProjectCommand.Execute(index);
                    vm.IsPlayProjectEnabled = false;
                    vm.IsStopProjectEnabled = true;
                    vm.IsPauseProjectEnabled = true;
                }

            }
        }

        //private void ic_MouseMove(object sender, MouseEventArgs e)
        //{
        //    var ellipse = sender as ItemsControl;
        //    if (ellipse != null && e.LeftButton == MouseButtonState.Pressed)
        //    {
        //        DragDrop.DoDragDrop(ellipse,
        //                             ellipse.Fill.ToString(),
        //                             DragDropEffects.Copy);
        //    }
        //}


    }
}
